package com.yf1706.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yf1706.bean.CheckDetail;
@Controller
@RequestMapping("up")
public class CheckUploadControllar {
	@RequestMapping(value = "upload", method = RequestMethod.POST)
    public ModelAndView uploadFileAction(@RequestParam("uploadFile") MultipartFile uploadFile,HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        InputStream fis = null;
        OutputStream outputStream = null;
        try {
            fis = uploadFile.getInputStream();
            outputStream = new FileOutputStream("D:\\img\\"+uploadFile.getOriginalFilename());
            String  url="D:\\img\\"+uploadFile.getOriginalFilename();
            HttpSession session=request.getSession();
            session.setAttribute("url",url);
            IOUtils.copy(fis,outputStream);
            return modelAndView;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        modelAndView.addObject("sucess", "上传失败!");
        return modelAndView;
	}
	//文件的下载
	 @RequestMapping("downloadFileAction")
	    public void downloadFileAction(HttpServletRequest request, HttpServletResponse response) {
	 
	        response.setCharacterEncoding(request.getCharacterEncoding());
	        response.setContentType("application/octet-stream");
	        FileInputStream fis = null;
	        try {
	            File file = new File("G:\\config.ini");
	            fis = new FileInputStream(file);
	            response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
	            IOUtils.copy(fis,response.getOutputStream());
	            response.flushBuffer();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (fis != null) {
	                try {
	                    fis.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	 }
}
